package com.market.controller;

import com.market.common.exception.ControllerException;
import com.market.common.vo.ResponseCode;
import com.market.common.vo.ResponseResult;
import com.market.entity.Brand;
import com.market.service.BrandService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Resource
    BrandService brandService;

    /**
     * 添加品牌功能
     * @param brandName
     * @return
     * @throws ControllerException
     */
    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestParam String brandName) throws ControllerException {
        int inser = brandService.addBrand(brandName);
        return ResponseResult.success(ResponseCode.SUCCESS, inser);
    }

    /**
     * 修改品牌功能
     * @param brandId
     * @param brandName
     * @return
     * @throws ControllerException
     */
    @PostMapping("/changer")
    public ResponseResult<Integer> update(@RequestParam Integer brandId, @RequestParam String brandName) throws ControllerException {
        int change = brandService.changeBrand(brandId, brandName);
        return ResponseResult.success(ResponseCode.SUCCESS, change);
    }

    /**
     * 删除品牌功能
     * @param brandId
     * @return
     * @throws ControllerException
     */
    @GetMapping("/delete")
    public ResponseResult<Integer> del(@RequestParam Integer brandId) throws ControllerException {
        int delete = brandService.deleteBrand(brandId);
        return ResponseResult.success(ResponseCode.SUCCESS, delete);
    }

    /**
     * 品牌搜索功能
     *
     * @param namesOrId
     * @return
     */
    @GetMapping("/search")
    public ResponseResult<List<Brand>> serch(@RequestParam String namesOrId) {
        List<Brand> brands = brandService.searchClasses(namesOrId);
        return ResponseResult.success(ResponseCode.SUCCESS, brands);
    }
}
